use std::path::PathBuf;
use chrono::Utc;
use chrono::Datelike;
use structopt::StructOpt;
use anyhow::Result;

mod proof;
mod verifier;
mod utils;
mod circuit;

use proof::generator::ProofGenerator;
use verifier::checker::AgeRangeChecker;
use utils::id_parser::IdCardParser;

#[derive(Debug, StructOpt)]
#[structopt(name = "age-range-zkp", about = "年龄范围零知识证明系统")]
struct Opt {
    /// 身份证号码
    #[structopt(short, long)]
    id_card: String,

    /// 最小年龄
    #[structopt(short, long, default_value = "18")]
    min_age: u32,

    /// 最大年龄
    #[structopt(short, long, default_value = "35")]
    max_age: u32,

    /// 电路文件路径
    #[structopt(long, parse(from_os_str))]
    circuit: PathBuf,
}

#[tokio::main]
async fn main() -> Result<()> {
    let opt = Opt::from_args();
    
    // 1. 解析身份证
    let parser = IdCardParser::new(&opt.id_card);
    let birth_year = parser.get_birth_year()?;
    
    // 2. 初始化证明生成器
    let generator = ProofGenerator::new(&opt.circuit)?;
    
    // 3. 生成证明
    let proof = generator.generate_proof(
        birth_year,
        opt.min_age,
        opt.max_age,
    )?;
    
    // 4. 验证证明
    let checker = AgeRangeChecker::new(&opt.circuit)?;
    let valid = checker.verify_proof(&proof,
        birth_year,
        opt.min_age,
        opt.max_age,
        Utc::now().year() as u32,)?;
    
    if valid {
        println!("✅ 验证通过: 年龄在{}到{}岁之间", opt.min_age, opt.max_age);
    } else {
        println!("❌ 验证失败: 年龄不在指定范围");
    }
    
    Ok(())
} 